/*This file is part of Legend of the Dark Blade.

    Legend of the Dark Blade is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Legend of the Dark Blade is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser Public License for more details.

    You should have received a copy of the GNU Lesser Public License
    along with Legend of the Dark Blade.  If not, see <http://www.gnu.org/licenses/>.*/

#include "global.h"
//#include "ball.h"
//#include "main.cpp"

Map::Map(){
                          /*int map=  {{ 0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 1  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,1  ,1  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,1  ,1  ,1  ,1  ,1  ,1  ,2  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,}  ,
                                           { 3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,3  ,1  ,0  ,0  ,0  ,0  ,0  ,0  ,0  ,1  ,}};*/
                                           //map[15][0]=0;
                                           /*map[15][1]=1;
                                           map[15][2]=2;
                                           map[15][3]=3;*/
                                           /*map[15][4]=4;
                                           map[15][5]=5;
                                           map[15][6]=6;
                                           map[15][7]=7;
                                           map[15][8]=8;
                                           map[15][9]=9;
                                           map[15][10]=10;
                                           map[15][11]=11;
                                           map[15][12]=12;
                                           map[15][13]=13;
                                           map[15][14]=14;
                                           map[15][15]=15;
                                           map[15][16]=16;
                                           map[15][17]=17;
                                           map[15][18]=18;
                                           map[15][19]=19;
                                           map[15][20]=20;
                                           map[15][21]=21;*/
                                           //map[15][22]=22;
                                           //map[15][23]=23;
}

Map::~Map(){

}

void Map::load(char* file){
    ifstream inputFile;
	char myString[8000];
    inputFile.open(file);
	inputFile.getline(myString, 8000, '\n');
	string str = myString;

	int pos = str.find(",");
    int num = atoi(str.substr (0,pos).c_str());
    str = str.substr (pos+1);

    pos = str.find(",");
    spawn_x = atoi(str.substr (0,pos).c_str())*TILEWIDTH;
    str = str.substr (pos+1);

    pos = str.find(",");
    spawn_y = atoi(str.substr (0,pos).c_str())*TILEHEIGHT;
    str = str.substr (pos+1);

    //map [h] [w];

	int myInt[num*3+1];
	for(int x = 0; x<num*3+1; x++){
        int pos = str.find(",");
        myInt[x] = atoi(str.substr (0,pos).c_str());

        str = str.substr (pos+1);
	}
	for(int x = 0; x<num*3; x+=3){
	    map[myInt[x+1]][myInt[x]]=myInt[x+2];
	}
}

void Map::draw(){
     for(int x = 0; x<MAPWIDTH; x++){
                 for(int y = 0; y<MAPHEIGHT; y++){
                         int tile = map[y][x];
                         apply_surface(x*TILEWIDTH-cam_x,y*TILEHEIGHT-cam_y,tilesheet,screen,&clip[tile]);
                 }
     }
}
